package com.bos.controller;


import com.bos.common.StrUtil;
import com.bos.data.model.BosParamModel;
import com.bos.data.model.SalesModel;
import com.bos.service.LogsService;
import com.bos.service.SalesService;
import com.bos.common.PageData;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 *
 * 查询销售表中的数据
 *
 * */
@Controller
public class SalesController {
    @Autowired
    @Qualifier("salesServiceImpl")
    SalesService salesService;

    @Autowired
    private LogsService logsService;


    @RequestMapping(value = "findSales")
    public String findSales(Model model, HttpSession session, HttpServletRequest request) {
        //获取页面的当前页数和页面大小,默认是第一页
        String pageIndex = request.getParameter("pageNo");
        String setPageSize = request.getParameter("setPageSize");
        String dstatus= request.getParameter("dstatus");
        int pageNo = 1, pageSize = PageData.DEFAULTPAGESIZE;
        if (!Strings.isNullOrEmpty(pageIndex)) {
            pageNo = Integer.valueOf(pageIndex);
        }
        if (!Strings.isNullOrEmpty(setPageSize)) {
            pageSize = Integer.valueOf(setPageSize);
        }
        PageData allSales=salesService.findSales(null,null,0,0);
        PageData sales = salesService.findSales(null, null, pageSize, pageNo);
        model.addAttribute("list", sales);
        session.setAttribute("allSales", allSales);
        //调用typeName为商机状态的数据
        List<BosParamModel> status = salesService.findBos_params("商机状态");
        session.setAttribute("status", status);
        //调用typeName为业务类别的数据
        List<BosParamModel> yTypes = salesService.findBos_params("业务类别");
        session.setAttribute("yTypes", yTypes);
        //调用typeName为销售负责人的数据
        List<BosParamModel> leaders = salesService.findBos_params("销售负责人");
        session.setAttribute("leaders", leaders);
        if(Strings.isNullOrEmpty(dstatus)){
            model.addAttribute("dstatus","none");
        }else {
            model.addAttribute("dstatus",dstatus);
        }
        return "Sales";
    }


    //根据id删除数据
    @RequestMapping(value = "/deletByID")
    public String deleteById(Model model, @RequestParam("sid") int sid, HttpSession session) {
        int row = salesService.deleteById(sid);
        if (row > 0) {
            System.out.println("删除成功");
        }
        //插入日志
        logsService.insertLogs(session, "删除项目汇总记录");
        return "redirect:findSales";

    }

    //4,根据id查询出对应的数据
    @RequestMapping(value = "/findByID")
    public String findById(@RequestParam("id") int id, Model model, HttpSession session) {
        SalesModel sales = salesService.findSalsById(id);
        model.addAttribute("sa", sales);
        //调用typeName为商机状态的数据
        List<BosParamModel> status = salesService.findBos_params("商机状态");
        session.setAttribute("status", status);
        //调用typeName为业务类别的数据
        List<BosParamModel> yTypes = salesService.findBos_params("业务类别");
        session.setAttribute("yTypes", yTypes);
        //调用typeName为销售负责人的数据
        List<BosParamModel> leaders = salesService.findBos_params("销售负责人");
        session.setAttribute("leaders", leaders);
        return "jsp/updateSales";
    }

    //5.根据id修改数据
    @RequestMapping(value = "/updateByID", method = RequestMethod.POST)
    public String updateUserById(@ModelAttribute SalesModel salesModel,@RequestParam("money") String money, HttpSession session) {
        salesModel.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        //如果金额被修改成没有的时候
        if(!money.equals("")){
            BigDecimal bg = new BigDecimal(money);
            //调用service层的数据
            salesModel.setMoney(bg);
            //将修改的时间插入到sales表中

            int row = salesService.updateByiD(salesModel);
        }else{
            int row = salesService.updateByiD(salesModel);
        }
        //插入日志
        logsService.insertLogs(session, "修改项目汇总记录");
        return "redirect:findSales";
    }

    //6.写一个多条件查询，根据  负责人，，，，商机状态查询
    @RequestMapping(value = "/selectByAll", method = RequestMethod.POST)
    public String selectAll(HttpSession session, Model model, HttpServletRequest request,@RequestParam("dstatus") String dstatus) throws Exception {
        //获取页面的当前页数和页面大小,默认是第一页
        String pageIndex = request.getParameter("pageNo");
        String setPageSize = request.getParameter("setPageSize");
        int pageNo = 1, pageSize = PageData.DEFAULTPAGESIZE;
        if (!Strings.isNullOrEmpty(pageIndex)) {
            pageNo = Integer.valueOf(pageIndex);
        }
        if (!Strings.isNullOrEmpty(setPageSize)) {
            pageSize = Integer.valueOf(setPageSize);
        }
        //得到所有的负责人
        String[] sleader = request.getParameterValues("sleader");
        //得到所有的商机状态
        String[] status = request.getParameterValues("stype");
        if(sleader!=null){
            String leader=StrUtil.spliceStrFormList(sleader);
            model.addAttribute("sleader",leader);
        }
        if(status!=null){
            String stype=StrUtil.spliceStrFormList(status);
            model.addAttribute("stype",stype);
        }
        List<String> sleaderList = sleader != null ? Arrays.asList(sleader) : new ArrayList<>();
        List<String> statusList = status != null ? Arrays.asList(status) : new ArrayList<>();

        PageData sales = salesService.findSales(sleaderList, statusList, pageSize, pageNo);
        model.addAttribute("list", sales);
        if(Strings.isNullOrEmpty(dstatus)){
            session.setAttribute("dstatus","none");
        }else {
            session.setAttribute("dstatus",dstatus);
        }
        return "Sales";
    }


    //将数据插入到销售表中
    @RequestMapping(value = "/insertSales")
    public String insertSales(@RequestParam("status") String status, @RequestParam("sname") String sname
            , @RequestParam("stype") String stype, @RequestParam("money") String money
            , @RequestParam("sleader") String sleader, @RequestParam("content") String content
            , @RequestParam("point") String point, @RequestParam("target") String target, HttpSession session) {
        //将所有填入的数据插入到SalesModel表中
        SalesModel salesModel = new SalesModel();
        salesModel.setContent(content);
        BigDecimal bg = new BigDecimal(money);
        bg.setScale(4, BigDecimal.ROUND_HALF_UP);
        salesModel.setMoney(bg);
        salesModel.setPoint(point);
        salesModel.setSleader(sleader);
        salesModel.setSname(sname);
        salesModel.setPoint(point);
        salesModel.setStype(stype);
        salesModel.setTarget(target);
        salesModel.setStatus(status);
        //插入的时候将数据也插入到数据库中去
        salesModel.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        salesService.insertNewSales(salesModel);
        //插入日志
        logsService.insertLogs(session, "添加项目汇总记录");
        return "redirect:findSales";
    }

    @RequestMapping(value = "/exportSale/{type}")
    public String exportSale(HttpServletRequest request, Model model, @PathVariable("type") int type){
        //获取页面的当前页数和页面大小,默认是第一页
        String pageIndex = request.getParameter("pageNo");
        String setPageSize = request.getParameter("setPageSize");
        int pageNo = 1, pageSize = PageData.DEFAULTPAGESIZE;
        if (!Strings.isNullOrEmpty(pageIndex)) {
            pageNo = Integer.valueOf(pageIndex);
        }
        if (!Strings.isNullOrEmpty(setPageSize)) {
            pageSize = Integer.valueOf(setPageSize);
        }
        //得到所有的负责人
        String[] sleader = request.getParameterValues("sleader");
        //得到所有的商机状态
        String[] status = request.getParameterValues("stype");
        if(sleader!=null){
            String leader=StrUtil.spliceStrFormList(sleader);
            model.addAttribute("sleader",leader);
        }
        if(status!=null){
            String stype=StrUtil.spliceStrFormList(status);
            model.addAttribute("stype",stype);
        }
        List<String> sleaderList = sleader != null ? Arrays.asList(sleader) : new ArrayList<>();
        List<String> statusList = status != null ? Arrays.asList(status) : new ArrayList<>();
        if (type == 2) {
            PageData sales = salesService.findSales(sleaderList, statusList, 0, 0);
            model.addAttribute("list", sales);
        }else {
            PageData sales = salesService.findSales(sleaderList, statusList, pageSize, pageNo);
            model.addAttribute("list", sales);
        }
        return "sale/exportSale";
    }

    @RequestMapping(value = "copySales/{id}")
    public String copySales(@PathVariable("id") int id,HttpSession session){
        String name=salesService.copySales(id);
        //插入日志
        logsService.insertLogs(session, "复制一条项目汇总记录:["+name+"]");
        return "redirect:../findSales";
    }
}
